iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0
Python

Python和R入門語法比較系列 第 11

04 Python: pandas Series 數值資料 v R: 數值向量 [16th 鐵人 Day 11]

  • 分享至 

  • xImage
  •  

Python

Series

import pandas as pd

建立數列1 2 3 4

pd.Series([1,2,3,4])
    0    1
    1    2
    2    3
    3    4
    dtype: int64

建立數列 1 2 3 4... ...10

pd.Series(range(1,11))
    0     1
    1     2
    2     3
    3     4
    4     5
    5     6
    6     7
    7     8
    8     9
    9    10
    dtype: int64
s = pd.Series(range(1,11))
s
    0     1
    1     2
    2     3
    3     4
    4     5
    5     6
    6     7
    7     8
    8     9
    9    10
    dtype: int64

最大值..最小值..

  1. max
  2. min
  3. 標準差
  4. 平均數
  5. 中位數(第2分位)
  6. 第1分位數
  7. 第2分位數
s.max()
    10
s.std()
    3.0276503540974917
s.min()
    1
s.mean()
    5.5
s.median()
    5.5
s.quantile()
    5.5
s.quantile(0.25)
    3.25
s.quantile(0.75)
    7.75

個數

s.count()
    10
len(s)
    10

對 s 做 加減乘除

s+1
    0     2
    1     3
    2     4
    3     5
    4     6
    5     7
    6     8
    7     9
    8    10
    9    11
    dtype: int64
s-1
    0    0
    1    1
    2    2
    3    3
    4    4
    5    5
    6    6
    7    7
    8    8
    9    9
    dtype: int64
s*2
    0     2
    1     4
    2     6
    3     8
    4    10
    5    12
    6    14
    7    16
    8    18
    9    20
    dtype: int64
s/2
    0    0.5
    1    1.0
    2    1.5
    3    2.0
    4    2.5
    5    3.0
    6    3.5
    7    4.0
    8    4.5
    9    5.0
    dtype: float64

注意:原本的s沒變

R

#建立數列1 2 3 4
c(1,2,3,4)

#建立數列 1 2 3 4... ...10
c(1:10)

c = c(1:10)

#最大值, 最小值...
max(c)
min(c)
sd(c) # python 是 std
mean(c)
median(c)
quantile(c)[1] #python 是 quantile(0.0)
quantile(c)[2] #python 是 quantile(0.25)
quantile(c)[3] #python 是 quantile(0.5)
quantile(c)[4] #python 是 quantile(0.75)
#### 個數 ####
length(c)

#### 對c做 加減乘除
c+1
c-1
c*2
c/2
# *原本的c不變*

內容預告:

05 Python: Pandas Series 字串資料 v. R:文字向量

06 日期 in Python and R

07 Python 和 R 的 字串處理

08 [R] 用Regular Expression(正規表示法)處理文字

08 [python] 用Regular Expression(正規表示法)處理文字

09 [python] 表格 dataframe.insert插入欄位 和 字串處理 Series.str.split


上一篇
03 more about csv in Python and R [16th 鐵人 Day 10]
下一篇
05 Python: Pandas Series 字串資料 v. R:文字向量 [16th 鐵人 Day 12]
系列文
Python和R入門語法比較13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言